package com.ruoyi.common.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.ruoyi.common.config.BaseConfig;

import java.util.HashMap;
import java.util.Map;

public class AliPay {
        public static Map transfer(Map withdrawAlipay){
            Map result=new HashMap();
            try{
                // 初始化SDK
                AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
                // 构造请求参数以调用接口
                AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
                AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
                // 设置商家侧唯一订单号
                model.setOutBizNo(withdrawAlipay.get("out_business_no").toString());
                // 设置订单总金额
                model.setTransAmount(withdrawAlipay.get("amount").toString());
                // 设置描述特定的业务场景
                model.setBizScene("DIRECT_TRANSFER");
                // 设置业务产品码
                model.setProductCode("TRANS_ACCOUNT_NO_PWD");
                // 设置转账业务的标题
                model.setOrderTitle(withdrawAlipay.get("payer_show_name").toString());
                // 设置收款方信息
                Participant payeeInfo = new Participant();
                if(null==withdrawAlipay.get("payee_account")){
                    result.put("success","false");
                    //订单号
                    result.put("message","该用户支付宝收款账号未填写!");
                    return result;
                }
                payeeInfo.setIdentity(withdrawAlipay.get("payee_account").toString());
                if(null==withdrawAlipay.get("payee_real_name")){
                    result.put("success","false");
                    //订单号
                    result.put("message","该用户未进行实名认证!");
                    return result;
                }
                payeeInfo.setName(withdrawAlipay.get("payee_real_name").toString());
                payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
                model.setPayeeInfo(payeeInfo);
                // 设置业务备注
                model.setRemark("知识创作收益");
                // 设置转账业务请求的扩展参数
                request.setBizModel(model);
                AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
                System.out.println(response.getBody());
                if (response.isSuccess()) {
                    System.out.println("调用成功");
                    result.put("success",response.isSuccess());
                    //订单号
                    result.put("data",response.getOrderId());
                } else {
                    System.out.println("调用失败");
                    // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
                    // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
                    // System.out.println(diagnosisUrl);
                    result.put("success","false");
                    result.put("code",response.getSubCode() );
                    //订单号
                    result.put("message",response.getSubMsg());
                }
                return result;
            }catch (AlipayApiException e){
                result.put("success","false");
                result.put("code",e.getErrCode());
                result.put("message",e.getMessage());
                return result;
            }
        }
    private static AlipayConfig getAlipayConfig() {
        String privateKey  = BaseConfig.AliPayInit.merchantPrivateKey;
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(BaseConfig.AliPayInit.appId.trim());
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setFormat("json");
        alipayConfig.setAppCertPath(BaseConfig.AliPayInit.merchantCertPath);
        alipayConfig.setAlipayPublicCertPath(BaseConfig.AliPayInit.alipayCertPath);
        alipayConfig.setRootCertPath(BaseConfig.AliPayInit.alipayRootCertPath);
        return alipayConfig;
    }
}
